/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.online;

import com.google.gdata.data.photos.PhotoEntry;
import configuration.ConfigurationManager;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import picasaphotomanager.PicasawebClient;
import sync.exceptions.MergeException;
import sync.merge.IMergeProvider;

/**
 *
 * @author zalem1
 */
public class PicasaWebAlbumMergeProvider implements IMergeProvider {
    private HashMap<String, PicasaWebAlbumMediaContentPicture> _mediaContentCollection = new HashMap<String, PicasaWebAlbumMediaContentPicture>();
    private HashMap<String, String> _contentOwnership;
    
    public PicasaWebAlbumMergeProvider(HashMap<String, String> contentOwnership) {
        _contentOwnership = contentOwnership;
    }
    
    @Override
    public void changeLocalPath(String localPath, String newLocalPath) {
        synchronized (this) {
            PicasaWebAlbumMediaContentPicture mediaContent = _mediaContentCollection.get(localPath);
            _mediaContentCollection.remove(localPath);
            _mediaContentCollection.put(newLocalPath, mediaContent);
        }
        
    }
    

    @Override
    public boolean isChanged(String localPath) throws MergeException {
        PicasaWebAlbumMediaContentPicture mediaContent = getMediaContent(localPath);
        
        int length = 0;
        InputStream in = getThumbnailInputStream(localPath, length);
        String onlineHash = PicasawebClient.getHashForUrlResource(in, length);
        
        String dbHash = mediaContent.getHash();
        boolean changed = !dbHash.equals(onlineHash);
        
        return changed;
    }

    @Override
    public PicasaWebAlbumMediaContentPicture getMediaContent(String localPath) throws MergeException {
        PicasaWebAlbumMediaContentPicture mediaContent = _mediaContentCollection.get(localPath);
        if (mediaContent != null) return mediaContent;
        
        String albumPath = PicasaWebAlbumUtils.getParentPath(localPath);
        String owner = getContentOwner(localPath);
        String imageResolution = ConfigurationManager.GetPreferences().getDefaultResolution();
        PhotoEntry photo = PicasaWebAlbumCache.getMediaEntry(albumPath, owner, "72u", imageResolution);
        mediaContent = new PicasaWebAlbumMediaContentPicture(photo);
        return mediaContent;
    }

    @Override
    public boolean exists(String localPath) throws MergeException {
        PicasaWebAlbumMediaContentPicture media = getMediaContent(localPath);
        
        if (media != null && media.getMediaUrl() != null)
            return true;
        
        return false;
    }

    @Override
    public boolean isAlbum(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isPhoto(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isVideo(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isMedia(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getFilename(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isNew(String localPath) {
        return _mediaContentCollection.containsKey(localPath);
    }

    @Override
    public long getSize(String localPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getMediaHash(String localPath) throws MergeException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private InputStream getInputStreamByUrl(String Url, int length) throws MergeException {
        try {
            URL mediaUrl = new URL(Url);
            URLConnection connection = mediaUrl.openConnection();
            length = connection.getContentLength();
            InputStream stream = mediaUrl.openStream();
            
            return new BufferedInputStream(stream);
        } catch (MalformedURLException ex) {
            throw new MergeException("The URL is in the incorrect format", ex);
        } catch (IOException ex) {
            throw new MergeException("Cannot read data from the remote location", ex);
        }        
    }
    
    private InputStream getThumbnailInputStream(String localPath, final int length) throws MergeException {
        PicasaWebAlbumMediaContentPicture mediaContent = getMediaContent(localPath);
        InputStream in = getInputStreamByUrl(mediaContent.getThumbnailUrl(), length);
        
        return in;
    }    

    
    @Override
    public InputStream getInputStream(String localPath, final int length) throws MergeException {
        PicasaWebAlbumMediaContentPicture mediaContent = getMediaContent(localPath);
        InputStream in = getInputStreamByUrl(mediaContent.getMediaUrl(), length);
        
        return in;
    }
    
    @Override
    public OutputStream getOutputStream(String localPath) throws MergeException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updateMetadata(String localPath) throws MergeException {
        PicasaWebAlbumMediaContentPicture mediaContent = getMediaContent(localPath);

        int length = 0;
        InputStream in = getInputStream(localPath, length);
        String newHash = PicasawebClient.getHashForUrlResource(in, length);
        mediaContent.setHash(newHash);      
    }

    @Override
    public void delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private String getContentOwner(String localPath) {
        String owner = null;
        for (String path : _contentOwnership.keySet()) {
            if (path.equals(localPath)) {
                owner = _contentOwnership.get(path);
                break;
            }
        }
        return owner;
    }
}



